package com.zwl.sjzmanagement.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwl.execption.ZWLException;
import com.zwl.model.*;
import com.zwl.sjzmanagement.mapper.TqcMapper;
import com.zwl.sjzmanagement.service.TqcService;
import com.zwl.vo.TqcUpdateNeedsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TqcServiceImpl extends ServiceImpl<TqcMapper, TqcUpdateNeedsVo> implements TqcService {

    @Autowired
    private TqcMapper tqcMapper;


    @Override
    public Map<String, List<TqcUpdateNeedsVo>> tqcList() {
        Map<String, List<TqcUpdateNeedsVo>> resultMap = new HashMap<>();

        List<Tqc> tqcList = tqcMapper.queryTqc();

        for (Tqc tqc : tqcList) {
            String tqcName = tqc.getName();
            Integer totalLevel = tqc.getTotalLevel();

            // 为每个Tqc创建一个列表
            List<TqcUpdateNeedsVo> tqcUpdateNeedsVoList = new ArrayList<>();

            for (Integer level = 1; level <= totalLevel; level++) {
                // 每集对应需要的升级材料
                List<TqcMaterialsNeed> tqcMaterialsNeedList = tqcMapper.queryTqcMaterialsNeed(tqcName, level);
                // 基本升级数据
                TqcUpgrades tqcUpgrade = tqcMapper.queryTqcUpgrade(tqcName, level);


                // 构建返回数据
                TqcUpdateNeedsVo tqcUpdateNeedsVo = new TqcUpdateNeedsVo();
                tqcUpdateNeedsVo.setId(tqcUpgrade.getId());
                tqcUpdateNeedsVo.setProductionLocation(tqcName);
                tqcUpdateNeedsVo.setUpgradePrerequisites(tqcUpgrade.getUpgradePrerequisites());
                tqcUpdateNeedsVo.setLevel(level);
                tqcUpdateNeedsVo.setRequiredHafuCoins(tqcUpgrade.getRequiredHafuCoins());
                tqcUpdateNeedsVo.setTotalMaterialValue(tqcUpgrade.getTotalMaterialValue());
                tqcUpdateNeedsVo.setTotalUpgradeCost(tqcUpgrade.getTotalUpgradeCost());

                tqcUpdateNeedsVo.setTqcMaterialsNeeds(tqcMaterialsNeedList);

                tqcUpdateNeedsVo.setUpdatedAt(tqcUpgrade.getUpdatedAt());
                tqcUpdateNeedsVo.setCreatedAt(tqcUpgrade.getCreatedAt());

                tqcUpdateNeedsVoList.add(tqcUpdateNeedsVo);
            }

            // 将该Tqc的所有级别数据放入结果Map
            resultMap.put(tqcName, tqcUpdateNeedsVoList);
        }

        return resultMap;
    }




    @Override
    public void tqcAddOrUpdate(TqcUpdateNeedsVo tqcUpdateNeedsVo) {
        if (tqcMapper.queryTqcByName(tqcUpdateNeedsVo.getProductionLocation()) == null) {
            throw new ZWLException(201, "没有这个特勤处");
        }

        if (tqcMapper.queryById(tqcUpdateNeedsVo.getId()) != null) {
            //更新
            TqcUpgrades tqcUpgrades = new TqcUpgrades();
            BeanUtils.copyProperties(tqcUpdateNeedsVo, tqcUpgrades);
            tqcMapper.updateTqcUpgrades(tqcUpgrades);

            TqcMaterialsNeed tqcMaterialsNeed = new TqcMaterialsNeed();
            for (TqcMaterialsNeed materialsNeed : tqcUpdateNeedsVo.getTqcMaterialsNeeds()) {
                BeanUtils.copyProperties(materialsNeed, tqcMaterialsNeed);
                tqcMapper.updateTqcMaterialsNeed(tqcMaterialsNeed);
            }

        } else {
            //新增
            TqcUpgrades tqcUpgrades = new TqcUpgrades();
            BeanUtils.copyProperties(tqcUpdateNeedsVo, tqcUpgrades);
            tqcMapper.addTqcUpgrades(tqcUpgrades);

            TqcMaterialsNeed tqcMaterialsNeed = new TqcMaterialsNeed();
            for (TqcMaterialsNeed materialsNeed : tqcUpdateNeedsVo.getTqcMaterialsNeeds()) {
                BeanUtils.copyProperties(materialsNeed, tqcMaterialsNeed);
                tqcMapper.addTqcMaterialsNeed(tqcMaterialsNeed);
            }

            String tqcName = tqcUpgrades.getProductionLocation();
            Integer level = tqcUpgrades.getLevel();

            tqcMapper.updateTqc(tqcName,level);

        }
    }

    @Override
    public void deleteTqcById(Integer id) {

        try{

            TqcUpgrades tqcUpgrades = tqcMapper.queryById(id);
            String tqcName = tqcUpgrades.getProductionLocation();
            Integer level = tqcUpgrades.getLevel();
            tqcMapper.deleteTqcUpgradesById(id);


            tqcMapper.deleteTqcMaterialsNeedByName(tqcName,level);
            tqcMapper.updateTqc(tqcName,level-1);
        } catch (Exception e){
            throw new ZWLException(201,"tqc删除失败");
        }

    }

    @Override
    public void BatchdeleteTqcById(List<Integer> ids) {
        for (Integer id : ids) {
            try{

                TqcUpgrades tqcUpgrades = tqcMapper.queryById(id);
                String tqcName = tqcUpgrades.getProductionLocation();
                Integer level = tqcUpgrades.getLevel();
                tqcMapper.deleteTqcUpgradesById(id);
                tqcMapper.deleteTqcMaterialsNeedByName(tqcName,level);
            } catch (Exception e){
                throw new ZWLException(201,"tqc批量删除失败");
            }
        }
    }

    @Override
    public TqcUpdateNeedsVo getTqcById(Integer id) {
        TqcUpgrades tqcUpgrades = tqcMapper.queryById(id);
        TqcUpdateNeedsVo tqcUpdateNeedsVo = new TqcUpdateNeedsVo();

        Integer level = tqcUpgrades.getLevel();
        String tqcName = tqcUpgrades.getProductionLocation();
        BeanUtils.copyProperties(tqcUpgrades, tqcUpdateNeedsVo);
        List<TqcMaterialsNeed> tqcMaterialsNeedList = tqcMapper.queryTqcMaterialsNeed(tqcName, level);
        tqcUpdateNeedsVo.setTqcMaterialsNeeds(tqcMaterialsNeedList);
        return tqcUpdateNeedsVo;
    }
}
